<?php
class UsuariosController extends AppController {

	var $name = 'Usuarios';
	var $helpers = array('Html', 'Form');

	function index() {
		$this->Usuario->recursive = 0;
		$this->set('usuarios', $this->paginate());
		print_r($this->Session);
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Usuario.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('usuario', $this->Usuario->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
				$this->data['Usuario']['passwordlogin']=md5($this->data['Usuario']['passwordlogin']);
				$this->data['Usuario']['passwordtransaccion']=md5($this->data['Usuario']['passwordtransaccion']);
			$this->Usuario->create();
			if ($this->Usuario->save($this->data)) {
				$this->Session->setFlash(__('The Usuario has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Usuario could not be saved. Please, try again.', true));
			}
		}
		$grupos = $this->Usuario->Grupo->find('list');
		$this->set(compact('grupos'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Usuario', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Usuario->save($this->data)) {
				$this->Session->setFlash(__('The Usuario has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Usuario could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Usuario->read(null, $id);
		}
		$grupos = $this->Usuario->Grupo->find('list');
		$this->set(compact('grupos'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Usuario', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Usuario->del($id)) {
			$this->Session->setFlash(__('Usuario deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

    function beforeFilter()
    {
        $this->__validateLoginStatus();
    }

    function login()
    {
        if(empty($this->data) == false)
        {
            if(($usuario = $this->Usuario->validateLogin($this->data['Usuario'])) == true)
            {
                $this->Session->write('Usuario', $usuario);
                $this->Session->setFlash('Accesaste satisfactoriamente');
                $this->redirect('index');
                exit();
            }
            else
            {
                $this->Session->setFlash('Lo sentimos, la informacion que proporcionaste es incorrecta.');
                $this->redirect('login');
                exit();
            }
        }
    }

    function logout()
    {
        $this->Session->destroy('usuario');
        $this->Session->setFlash('You\'ve successfully logged out.');
        $this->redirect('login');
    }

    function __validateLoginStatus()
    {
        if($this->action != 'login' && $this->action != 'logout')
        {
            if($this->Session->check('Usuario') == false)
            {
                $this->redirect('login');
                $this->Session->setFlash('The URL you\'ve followed requires you login.');
            }
        }
    }


}
?>